package com.multithreading.algorithm;

import static java.util.Arrays.binarySearch;

/**
 * @author zx
 * @version 1.0
 * @package_name: com.multithreading.algorithm
 * @date 2024/6/19 19:45
 */
// 二分查找
public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {1, 3, 8, 10, 11, 67, 100, 123, 125, 134};

        int target = 100;

        int head = 0;

        int end = arr.length - 1;

        boolean flag = false;

        while (head <= end) {
            int mid = (head + end) / 2;
            if (arr[mid] == target) {
                System.out.println("找到了，下标为：" + mid);
                flag = true;
                break;
            } else if (target > arr[mid]) {
                head = mid + 1;
            } else {
                end = mid - 1;
            }
        }

        if (!flag){
            System.out.println("没有找到");
        }
    }
}
